#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
struct Node {
  char u, v;
} e[1005];
int head[128], deg[N];
int ver[1005], Next[1005], tot;
void add(int u, int v) {
  ++tot;
  ver[tot] = v;
  Next[tot] = head[u];
  head[u] = tot;
}
char st[128], top;
int toposort(int x) {
  top = 0;
  queue<int> q;
  rep(i, 'A', 'A' + n - 1) if (!deg[i]) q.push(i);
  int hcnt = 0;
  while (q.size()) {
    hcnt = max<int>(hcnt, q.size());
    int u = q.front();
    q.pop();
    st[++top] = u;
    for (int i = head[u]; i; i = Next[i]) {
      int v = ver[i];
      deg[v]--;
      if (!deg[v]) q.push(v);
    }
  }
  if (top < n) {
    cout << "Inconsistency found after " << x << " relations.";
    exit(0);
  } else if (hcnt == 1) {
    cout << "Sorted sequence determined after " << x << " relations: ";
    rep(i, 1, top) cout << st[i];
    cout << '.';
    exit(0);
  } else
    return 0;
}
int check(int x) {
  tot = 0;
  rep(i, 'A', 'Z') deg[i] = 0, head[i] = 0;
  rep(i, 1, x) add(e[i].u, e[i].v), deg[e[i].v]++;
  return toposort(x);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, m) {
    char op;
    cin >> e[i].u >> op >> e[i].v;
  }
  rep(i, 1, m) check(i);
  cout << "Sorted sequence cannot be determined.";
  return 0;
}